#include "fileinfo.h"
#include "ui_fileinfo.h"
#include <QFileDialog>
#include <QDateTime>
#include <QFile>
#include <QDir>
#include <QMimeData>
#include <QClipboard>
#include <QInputDialog>
#include <QDebug>
#include <QStringList>

Fileinfo::Fileinfo(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Fileinfo)
{
    ui->setupUi(this);
    setWindowTitle("文件信息获取");

}

Fileinfo::~Fileinfo()
{
    delete ui;
}

void Fileinfo::getFileInfo(QString &fileName)
{

    QFileInfo info(fileName);
    //获取文件大小
    int size = info.size();
    QString Size=QString::number(size)+"B";
    ui->lineEdit_2->setText(Size);
    //获取创建时间
    QDateTime crTime = info.birthTime();
    QString  Time = crTime.toString("yyyy-MM-dd hh:mm:ss");
    ui->lineEdit_3->setText(Time);
    //获取最后修改时间
    QDateTime xTime = info.lastModified();
    QString  strXTime = xTime.toString("yyyy-MM-dd hh:mm:ss");
    ui->lineEdit_4->setText(strXTime);
    //获取最后访问时间
    QDateTime FTime= info.lastRead();
    QString  strFTime = FTime.toString("yyyy-MM-dd hh:mm:ss");
    ui->lineEdit_5->setText(strFTime);

    //文件属性信息
    bool isdir = info.isDir();
    bool isfile = info.isFile();
    bool issymlink = info.isSymLink();
    bool ishidden = info.isHidden();
    bool isread = info.isReadable();
    bool iswritable = info.isWritable();
    bool isexcutable = info.isExecutable();

    ui->isMuliBox->setChecked(isdir);
    ui->isFuhaoBox->setChecked(isfile);
    ui->isFuhaoBox->setChecked(issymlink);
    ui->isYincangBox->setChecked(ishidden);
    ui->isDuBox->setChecked(isread);
    ui->isXieBox->setChecked(iswritable);
    ui->isZhixingBox->setChecked(isexcutable);
}

//获取文件
void Fileinfo::on_pushButton_clicked()
{
    fileName = QFileDialog::getOpenFileName(this,"打开文件",".","files(*)");  //选择文件或文本
    //fileName = QFileDialog::getExistingDirectory(this,"选择目录",".");        //选择目录（文件夹）
    ui->lineEdit->setText(fileName);
    getFileInfo(fileName);
}
//新建
void Fileinfo::on_pushButton_2_clicked()
{

    QFileInfo info(fileName);
    if(info.isDir())
    {
        bool ok;
        QString text = QInputDialog::getText(this, tr("新建文件"),tr("请输入文件名："), QLineEdit::Normal,0, &ok);
        QDir path;
        QString str=fileName+"/"+text;
        path.mkdir(str);
    }
    else if(info.isFile())
    {
        bool ok;
        QString text = QInputDialog::getText(this, tr("新建文件"),tr("请输入文件名：(.txt)"), QLineEdit::Normal,0, &ok);
        QString str=0;
        QStringList ss;
        ss=fileName.split("/");
        for(int i=0;i<ss.size()-1;i++)
        {

            str=str+ss.at(i)+"/";
        }
        str=str+text+".txt";
        qDebug () <<str;
        QFile s(str);
        s.open(QIODevice::WriteOnly);
        s.close();
    }
}
//复制
void Fileinfo::on_pushButton_3_clicked()
{
    QList<QUrl> copyfile;
        QUrl url=QUrl::fromLocalFile(fileName);    //待复制的文件
        if(url.isValid()){
            copyfile.push_back(url);
        }else{
            return;
        }
        QMimeData *data=new QMimeData;
        data->setUrls(copyfile);

        QClipboard *clip=QApplication::clipboard();
        clip->setMimeData(data);
}
//删除
void Fileinfo::on_pushButton_5_clicked()
{
    QFileInfo info(fileName);
    if(info.isDir())
    {
        QDir path;
        path.remove(fileName);
    }
    else if(info.isFile())
    {
        QFile path;
        path.remove(fileName);
    }

}

void Fileinfo::on_pushButton_6_clicked()
{
    fileName = QFileDialog::getExistingDirectory(this,"选择目录",".");        //选择目录（文件夹）
    ui->lineEdit->setText(fileName);
    getFileInfo(fileName);
}
